package com.ktwlsoft.archivemanage.controller.study;

import com.ktwlsoft.archivemanage.domain.business.businessService.study.StuduClassifyService;
import com.ktwlsoft.archivemanage.domain.business.businessService.study.StudyCommentService;
import com.ktwlsoft.archivemanage.domain.dao.entity.study.StudyClassify;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.study.AddClassifyRequest;
import com.ktwlsoft.archivemanage.payload.study.UpdateClassifyRequest;
import com.ktwlsoft.archivemanage.payload.study.studyClass.AddCommentRequest;
import com.ktwlsoft.archivemanage.payload.study.studyClass.ClassManagePageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author JokerMo
 * @version 1.0
 * @date 2019/12/25 9:55
 */
@Api(value = "课程分类管理", description = "课程分类管理 By 莫伯华")
@RestController
@RequestMapping("/study/classify")
public class StudyClassifyController {
    private final StuduClassifyService studuClassifyService;
    private final StudyCommentService studyCommentService;


    public StudyClassifyController(StuduClassifyService studuClassifyService, StudyCommentService studyCommentService) {
        this.studuClassifyService = studuClassifyService;
        this.studyCommentService = studyCommentService;
    }

    @PostMapping("/addClassify")
    @ResponseBody
    @ApiOperation("添加课程分类")
    public ResponseEntity<?> addClassify(@Valid @RequestBody AddClassifyRequest request) {
        studuClassifyService.insert(request);
        return ResponseEntity.ok(Result.ok("操作完成"));
    }

    @PostMapping("/updateClassify")
    @ResponseBody
    @ApiOperation("编辑课程分类")
    public ResponseEntity<?> updateClassify(@Valid @RequestBody UpdateClassifyRequest request) {
        studuClassifyService.update(request);
        return ResponseEntity.ok(Result.ok("操作完成"));
    }

    @DeleteMapping("/deleteClassify")
    @ResponseBody
    @ApiOperation("删除课程分类")
    public ResponseEntity<?> deleteClassify(@ApiParam(name = "id", value = "课程分类id") @RequestParam Long id) {
        studuClassifyService.deleteById(id);
        return ResponseEntity.ok(Result.ok("操作完成"));
    }

    @GetMapping("/list")
    @ResponseBody
    @ApiOperation("课程分类列表")
    public ResponseEntity<?> list(@ApiParam(name = "classType", value = "课程类型, MANAGE管理端, USER用户端") @RequestParam(defaultValue = "", required = true) StudyClassify.ClassType classType) {
        return ResponseEntity.ok(studuClassifyService.list(classType));
    }

    @GetMapping("/classManagePage")
    @ResponseBody
    @ApiOperation("课程列表分页")
    public ResponseEntity<?> classManagePage(ClassManagePageRequest request) {
        return ResponseEntity.ok(studuClassifyService.classManagePage(request));
    }

    @PostMapping("/addComment")
    @ResponseBody
    @ApiOperation("添加评论")
    public ResponseEntity<?> addComment(@Valid @RequestBody AddCommentRequest request) {
        studyCommentService.insert(request);
        return ResponseEntity.ok(Result.ok("操作完成"));
    }

    @GetMapping("/getClassComments")
    @ResponseBody
    @ApiOperation("课程管理评论分页")
    public ResponseEntity<?> getClassComments(@RequestParam(defaultValue = "1") int pn,
                                              @RequestParam(defaultValue = "30") int pageSize, @ApiParam(name = "classId", value = "课程id") @RequestParam(defaultValue = "", required = true) Long classId) {
        return ResponseEntity.ok(studyCommentService.getClassComments(pn, pageSize, classId));
    }

    @GetMapping("/auditComment")
    @ResponseBody
    @ApiOperation("评论审核")
    public ResponseEntity<?> auditComment(@ApiParam(name = "id", value = "评论id") @RequestParam("id") Long id,
                                          @ApiParam(name = "passed", value = "是否通过 true:通过,false;不通过") @RequestParam("passed") Boolean passed) {
        studyCommentService.auditComment(id, passed);
        return ResponseEntity.ok(Result.ok("操作完成"));
    }

    @GetMapping("/getSubjectClasses")
    @ResponseBody
    @ApiOperation("获取专题课程列表")
    public ResponseEntity<?> getSubjectClasses(@RequestParam(defaultValue = "1") int pn,
                                               @RequestParam(defaultValue = "30") int pageSize,
                                               @ApiParam(name = "subjectId", value = "专题id") @RequestParam(defaultValue = "", required = true) Long subjectId,
                                               @ApiParam(name = "classType", value = "用户端（USER）、管理端（MANAGE）") @RequestParam String classType) {
        return ResponseEntity.ok(studuClassifyService.selectBySubjectId(pn, pageSize, subjectId, classType));
    }
}
